package com.zzx.bms.service.impl;

import com.zzx.bms.domain.*;
import com.zzx.bms.persistence.BookMapper;
import com.zzx.bms.persistence.JournalMapper;
import com.zzx.bms.service.IBookService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


/**
 * 
* @ClassName: BookServiceImpl
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author Admin
* @date 2015年4月20日 下午9:41:40 
*
 */
@Service(value="bookService")
public class BookServiceImpl implements IBookService {

    /** 
    * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么) 
    */ 
    
    private static final long serialVersionUID = 7522721238681998540L;
    
    @Autowired
    private BookMapper bookMapper;

	@Autowired
	private JournalMapper journalMapper;

	/**
	 * 列表展示
	 * @param book
	 * @return
	 */
	@Override
	public List<Book> findBookList(Book book) {
		BookExample exp = new BookExample();
		BookExample.Criteria crt = exp.createCriteria();
		crt.andIsDeleteEqualTo("0");
		exp.setOrderByClause("  update_date  DESC ");
		List<Book> list = bookMapper.selectByExample(exp);
		return list;
	}

	/**
	 * 新书入库
	 * @param book
	 * @return
	 */
	@Override
	public int saveBook(Book book) {
		int flag = 0;
		if(book.getId()!=null){
			book.setUpdateDate(new Date());
			flag = bookMapper.updateByPrimaryKeySelective(book);
		}
		else{
			book.setCreateDate(new Date());
			book.setIsDelete("0");
			book.setBorrowNum(0);
			book.setUpdateDate(new Date());
			flag = bookMapper.insertSelective(book);
		}
		return flag;
	}

	/**
	 * 新增校验 书籍是否存在
	 * @param book
	 * @return
	 */
	@Override
	public int checkBook(Book book) {
		BookExample exp = new BookExample();
		BookExample.Criteria crt = exp.createCriteria();

		if(StringUtils.isNotEmpty(book.getName()))
			crt.andNameEqualTo(book.getName().trim());
		if(StringUtils.isNotEmpty(book.getAuthor()))
			crt.andAuthorEqualTo(book.getAuthor().trim());
		if(StringUtils.isNotEmpty(book.getPress()))
			crt.andPressEqualTo(book.getPress().trim());
		if(book.getId()!=null)
			crt.andIdNotEqualTo(book.getId());
		int flag = bookMapper.countByExample(exp);
		return flag;
	}

	@Override
	public Book findBookById(String id) {
		Book book = null;
		if(StringUtils.isNotEmpty(id))
			book = bookMapper.selectByPrimaryKey(Integer.parseInt(id));
		return book;
	}

	@Override
	public int saveJournal(Journal book) {
		int flag = 0;
		if(book.getId()!=null){
			book.setUpdateDate(new Date());
			flag = journalMapper.updateByPrimaryKeySelective(book);
		}
		else{
			book.setCreateDate(new Date());
			book.setIsDelete("0");
			book.setBorrowNum(0);
			book.setUpdateDate(new Date());
			flag = journalMapper.insertSelective(book);
		}
		return flag;
	}

	@Override
	public Journal findJournalById(String id) {
		Journal book = null;
		if(StringUtils.isNotEmpty(id))
			book = journalMapper.selectByPrimaryKey(Integer.parseInt(id));
		return book;
	}

	@Override
	public int checkJournal(Journal book) {
		JournalExample exp = new JournalExample();
		JournalExample.Criteria crt = exp.createCriteria();

		if(StringUtils.isNotEmpty(book.getName()))
			crt.andNameEqualTo(book.getName().trim());
		if(StringUtils.isNotEmpty(book.getAuthor()))
			crt.andAuthorEqualTo(book.getAuthor().trim());
		if(StringUtils.isNotEmpty(book.getPress()))
			crt.andPressEqualTo(book.getPress().trim());
		if(book.getId()!=null)
			crt.andIdNotEqualTo(book.getId());
		int flag = journalMapper.countByExample(exp);
		return flag;
	}

	@Override
	public List<Journal> findJournalList(Journal book) {
		JournalExample exp = new JournalExample();
		JournalExample.Criteria crt = exp.createCriteria();
		crt.andIsDeleteEqualTo("0");
		exp.setOrderByClause("  update_date  DESC ");
		List<Journal> list = journalMapper.selectByExample(exp);
		return list;
	}

	@Override
	public int deleteBookById(String id) {
		int flag = 0;
		BookExample exp = new BookExample();
		BookExample.Criteria crt = exp.createCriteria();
		Book nursestation = bookMapper.selectByPrimaryKey(Integer.parseInt(id));
		if(nursestation!=null){
			nursestation.setIsDelete("1");
			flag = bookMapper.updateByPrimaryKeySelective(nursestation);
		}
		return flag;
	}

	@Override
	public int deleteJournalById(String id) {
		int flag = 0;
		JournalExample exp = new JournalExample();
		JournalExample.Criteria crt = exp.createCriteria();
		Journal nursestation = journalMapper.selectByPrimaryKey(Integer.parseInt(id));
		if(nursestation!=null){
			nursestation.setIsDelete("1");
			flag = journalMapper.updateByPrimaryKeySelective(nursestation);
		}
		return flag;
	}

	@Override
	public int getBookTotal() {
		BookExample exp = new BookExample();
		BookExample.Criteria crt = exp.createCriteria();
		crt.andIsDeleteEqualTo("0");
		int flag = bookMapper.countByExample(exp);

		JournalExample exp1 = new JournalExample();
		JournalExample.Criteria crt1 = exp1.createCriteria();
		crt1.andIsDeleteEqualTo("0");
		int flag1 = journalMapper.countByExample(exp1);

		return flag+flag1;
	}


}
